// 假数据
var newData = {
  date: "2019-03-15",
  Sales: "江门客运站",
  shifts: 6,
  order: 8,
  peoples: 28,
  trips: [
    {
      id: 111,
      time: "8:00",
      start: "江门客运站",
      end: "香港机场",
      pass: "珠海口岸",
      car: "粤ABC111",
      people: 5,
      price: 840
    },
    {
      id: 222,
      time: "8:10",
      start: "江门客运站",
      end: "珠海口岸",
      car: "粤ABC222",
      people: 3,
      price: 600
    },
    {
      id: 333,
      time: "8:20",
      start: "江门客运站",
      end: "香港迪斯尼",
      pass: "珠海口岸",
      car: "粤ABC333",
      people: 1,
      price: 168
    }
  ]
}

// 销售点详情
var salesList = {
    schedule: "#uppage_schedule",
    tripList: ".sales-trip",

    // 打开日历
    scheduleShow: function(){
        $(this.schedule).show();
    },

    // 关闭日历
    scheduleHide: function(){
        var _this = this;
        $(this.schedule).addClass("hide");
        setTimeout(function(){
            $(_this.schedule).removeClass("hide").hide();
        },500);
    },

    // 更新数据
    loadList: function(){
      var _this = this;
      var date = $("#sales_date").attr("date");
      $(this.tripList).empty();
      // todo 根据日期及站点信息，加载新数据newData
      var data = newData;

      // 填充数据
      $("[name=shifts]").text(data.shifts);
      $("[name=order]").text(data.order);
      $("[name=peoples]").text(data.peoples);
      $.each(data.trips,function(index,object){
        var psss = object.pass?object.pass: "";
        $(_this.tripList).append(
          '<li tripid="'+ object.id +'">'+
              '<em>'+ object.time +'</em>'+
              '<div class="trip-block">'+
                  '<div class="trip-head">'+
                      '<span>'+ object.car +'</span>'+
                      '<em>¥'+ object.price +'</em>'+
                  '</div>'+
                  '<div class="trip-body">'+
                      '<div class="left">'+
                          '<span>'+ object.start +'</span>'+
                          '<em>'+ psss +'</em>'+
                          '<span>'+ object.end +'</span>'+
                      '</div>'+
                      '<div class="right">'+
                          '<span>'+ object.people +'人乘车</span>'+
                      '</div>'+
                  '</div>'+
              '</div>'+
          '</li>'
        );
      });

      // 绑定班次点击
      $(_this.tripList).find("li").bind("click",function(){
        var tripid = $(this).attr("tripid");
        // todo 根据tripid跳转到对应班次详情页面

        $(location).attr('href', "tripDetail.html");
      })

    },

    // 初始化
    init: function() {
        var _this = this;

        $("#sales_date")
        .attr("date",datelist.today)
        .text(datelist.dateToText(datelist.today,true))
        .bind("click",function(){
            _this.scheduleShow();
        });

        // 关闭窗口
        $(this.schedule).find(".close").bind("click",function(){
            _this.scheduleHide();
        });

        // 加载数据
        this.loadList();
    }
};

// 日历
var indexSchedule = new Schedule({
    el: '#schedule-box',
    clickCb: function (y,m,d) {
        var month = m,
            day = d;
        if(m.toString().length==1) month = "0"+m;
        if(d.toString().length==1) day = "0"+d;
        var date = y+"-"+month+"-"+day;
        $('#sales_date')
        .attr("date",date)
        .text(datelist.dateToText(date,true));
        salesList.scheduleHide();
        salesList.loadList();
    }
});

salesList.init();
